# Author    : CoffeeChicken
# Date      : 2020-10-10 22:43
# Function  : 演示 下载图片

import requests
from lxml import etree

url_front = "http://picxxxx.top/page/{num}/"
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/85.0.4183.121 Safari/537.36"
}

all_img_links = []

for i in range(100):
    url = url_front.format(num=int(i + 1))
    response_front = requests.get(url, headers).text
    tree = etree.HTML(response_front)
    link_list = tree.xpath("//ul[@id='masonry']/li/a/@href")
    # print(link_list)
    img_links = []
    for links in link_list:
        response_page = requests.get(links, headers).text
        # print(response_page)
        tree1 = etree.HTML(response_page)
        img_links = tree1.xpath("//img/@src")
        all_img_links += img_links
        print(all_img_links)


# print(len(all_img_links))

for index in range(112, 112 + len(all_img_links)):
    response_pic = requests.get(all_img_links[index], headers)
    with open("more_sexy/" + str(int(index + 1)) + ".jpg", "wb") as fp:
        fp.write(response_pic.content)
    print("======>", index, "<=====", "is ok")
